'use strict';
const utils = require('../utils/utils');
const Service = require('egg').Service;

class ProductService extends Service {
  async list(params) {
    const where = {};
    if (params.classify_id) {
      const classifyData = await this.classify();
      const ids = utils.getChildrenIds(classifyData, params.classify_id);
      where.classify_id = ids;
    }
    const products = await this.app.mysql.select('product', {
      limit: params.pageSize * 1, // 返回数据量
      offset: params.pageSize * (params.page - 1), // 数据偏移量
      where,
    });
    return products;
  }

  async classify() {
    const classifyData = await this.app.mysql.select('classify');
    return utils.arrayToTree([ ...classifyData ]);
  }
}

module.exports = ProductService;
